package com.zmn.oms.admin.controller.work.modify;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.OrderTurnDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 转单
 * @author sunlife
 * @date 2019/6/4 19:38
 */
@Controller
@RequestMapping("/work/modify")
public class WorkTurnController extends WorkBaseController {

    @Resource
    private ZsOrderWorkBService zsOrderWorkBService;
    @Resource
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Resource
    private WorkFlowContextBService workFlowContextBService;

    @RequestMapping(value = "/turn")
    public ModelAndView turn(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/turn");

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);

        mv.addObject("platList", zsOrderWorkBService.getManagePlatList(orderWork, orderDetail));
        mv.addObject("orderPlatWork", orderWork.getPlatWork());
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);

        return mv;
    }

    /**
     * 保存转单信息
     */
    @RequestMapping(value = "/save/turn", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO saveTurn(@RequestBody OrderTurnDTO orderTurnDTO) {
        // 再次校验以防有人修改订单
        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderTurnDTO.getOrderId(), orderTurnDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_TURN);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        try {
            zsOrderWorkBService.saveTurn(orderTurnDTO);
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }

        // 转单后校验
        Boolean result = zsOrderWorkBService.checkTurnStatus(orderTurnDTO.getOrderId(),orderTurnDTO.getWorkId());
        if(!result){
            orderTurnDTO.setStatus(OrderStatusConsts.WORK_STATUS_INPUT);
            orderTurnDTO.setResultStatus(OrderStatusConsts.WORK_RESULT_DOING);
            orderTurnDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            orderTurnDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
            zsOrderWorkBService.changeTurnStatus(orderTurnDTO);
        }

        // 工单自动流转
        workFlowContextBService.asyncAutoFlow(orderTurnDTO.getOrderId(), orderTurnDTO.getWorkId());

        return ResultDTO.success();
    }

}
